/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import com.jnetx.javax.sip.util.Parametrized;

import javax.sip.header.Header;
import javax.sip.header.Parameters;
import java.text.ParseException;
import java.util.Iterator;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public abstract class ParametrizedHeaderBase extends HeaderBase implements Parameters {
    protected final Parametrized<Header> parametrized = createParametrized();

    protected ParametrizedHeaderBase(String name) {
        super(name);
    }

    protected ParametrizedHeaderBase(String name, byte[] data, int start, int end) {
        super(name, data, start, end);
    }

    protected Parametrized<Header> createParametrized() {
        return new Parametrized<Header>(this);
    }

    public String getParameter(String name) {
        parse();
        return parametrized.getParameter(name);
    }

    public void setParameter(String name, String value) throws ParseException {
        parse();
        parametrized.setParameter(name, value);
        invalidateHeaderData();
    }

    public void setParameter(String name, String value, Parametrized.QuotationType qType) throws ParseException {
        parse();
        parametrized.setParameter(name, value, qType);
        invalidateHeaderData();
    }

    public void setRawParameter(String name, String value) throws ParseException {
        parse();
        parametrized.setRawParameter(name, value);
        invalidateHeaderData();
    }

    public Iterator getParameterNames() {
        parse();
        return parametrized.getParameterNames();
    }

    public void removeParameter(String name) {
        parse();
        parametrized.removeParameter(name);
        invalidateHeaderData();
    }

    protected void cloneParameters(ParametrizedHeaderBase source) {
        parametrized.putParameters(source.parametrized.getParameters());
    }

    @Override
    protected StringBuilder getParametersAsString(StringBuilder sb, char delimiter) {
        final String str = parametrized.getParametersAsString(delimiter);
        if (str != null && !str.equals(""))
            return sb.append(delimiter).append(str);
        else
            return sb;
    }
}
/*
 * $Log$
 */